package middlewares

import (
	"app/app/controllers"
	"strings"

	"github.com/kataras/iris/v12"
)

func AuthFilter(ctx iris.Context) {
	if !strings.Contains(ctx.RequestPath(false), "api") {
		ctx.Next()
		return
	}
	_, roleid, _, err := controllers.GetUser(ctx)
	if err != nil || (strings.Contains(ctx.RequestPath(false), "user") && roleid != 1) {
		ctx.StatusCode(401)
		return
	}
	ctx.Next()
}
